package com.cqw.commontest.lambda;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-06-01 10:28
 * @Description: 聚合  max mix count
 */
public class StreamTest2 {


    public static void main(String[] args) {
        //1、获取String集合中最长的元素
        List<String> stringList = Arrays.asList("李磊", "LiLi", "韩梅梅", "qwe");
        String s = stringList.stream().max((o1, o2) -> o1.length() - o2.length()).get();
        String max = stringList.stream().max(Comparator.comparing(String::length)).get();
        System.out.println(max);
        //2、获取Integer集合中的最大值  Integer::compareTo
        List<Integer> integerList = Arrays.asList(1, 3, 2, 4, 6);
        //自然排序
        Integer maxInt = integerList.stream().max(Integer::compareTo).get();
        System.out.println(maxInt);

        Integer minInt = integerList.stream().min(Integer::compareTo).get();
        System.out.println(minInt);

        //自定义排序
        Integer integer = integerList.stream().max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        }).get();
        System.out.println(integer);

        //count  计数 找出大于2的元素的个数
        long count = integerList.stream().filter(t -> t > 2).count();


        //5、获取工资最高的员工
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 1000, 21, "男", "上海"));
        personList.add(new Person("李四", 2000, 25, "女", "安徽"));
        personList.add(new Person("王五", 2200, 36, "男", "杭州"));
        personList.add(new Person("赵六", 1400, 25, "女", "南京"));
        personList.add(new Person("曹七", 3000, 12, "男", "北京"));
        personList.add(new Person("Lily", 5000, 42, "女", "上海"));

        personList.stream().max(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getSalary() - o2.getSalary();
            }
        });

        personList.stream().max((o1, o2) -> o1.getSalary() - o2.getSalary()).get();


        Person person = personList.stream().max(Comparator.comparing(Person::getSalary)).get();


        String join = StringUtils.join(personList, ",");
        System.out.println(join);

    }


}
